package org.opengauss.sqltranslator;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.util.StringUtils;
import org.opengauss.sqltranslator.dialect.mysql.MySqlToOpenGaussOutputVisitor;
import org.opengauss.sqltranslator.dialect.mysql.util.SqlInstallTransformUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.util.List;



public class ExecuteTranslate {

    private static final Logger logger = LoggerFactory.getLogger(ExecuteTranslate.class);

    public static void main(String[] args) {
        String raw_sql =
                """
                        select table_name,table_comment
                                                                           from information_schema.TABLES
                                                                            WHERE  table_schema = ?
                                                                               and table_type = 'BASE TABLE'
                                                                                   and table_name IN
                                                                                   (
                                                                                       ?
                                                                                   )
                        """;
        boolean column_case_sensitive = false;
        byte[] decode =raw_sql.getBytes(StandardCharsets.UTF_8);
        raw_sql = new String(decode, StandardCharsets.UTF_8);
        try {
            System.out.println(translateMysql2openGauss(raw_sql, true, column_case_sensitive));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        System.out.println("---ExecuteTranslate已加载---");
    }


    public static String   translateMysql2openGauss(String Sql_in, boolean debug,boolean column_case_sensitive) {
        byte[] decode = Sql_in.getBytes(StandardCharsets.UTF_8);
        Sql_in = new String(decode, StandardCharsets.UTF_8);
        String sql = SqlInstallTransformUtil.getSql(Sql_in);
        if(!StringUtils.isEmpty(sql)){
            return sql;
        }
        final StringBuilder appender = new StringBuilder();
        final MySqlToOpenGaussOutputVisitor visitor = new MySqlToOpenGaussOutputVisitor(appender,
                column_case_sensitive);

        final List<SQLStatement> sqlStatements = SQLUtils.parseStatements(Sql_in, DbType.mysql);

        final StringBuilder res = new StringBuilder();

        for (SQLStatement statement : sqlStatements) {
            statement.accept(visitor);
            visitor.println();
            if (debug) {
                System.out.println(appender.toString());
            } else {
                res.append(appender.toString());
            }
            appender.delete(0, appender.length());
        }

        return res.toString();
    }

}
